package anshe.widget
{
	import anshe.event.AnsheNetEvent;
	import anshe.struct.ActionStruct;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;

	public class AnsheSuperClient extends Sprite
	{
		private var nowGameName:String	= "";	//	游戏名称（不同的游戏必须用不同的游戏名）
		private var ansheSpace:AnsheNetSpace;	//	NC 连接对象
		private var fDebug:Object;				//	调试框对象
		
		protected var msgArray:Array;	//	要发送的消息列表
		protected var space:Object;		//	一个 AnsheSprite 类实例
		
		public function AnsheSuperClient(gName:String)
		{
			this.msgArray = new Array();
			this.gameName = gName;
			
			trace("InitSuperClient");
			
			this.addEventListener(Event.ADDED_TO_STAGE, onInitClient);
			this.addEventListener(Event.ENTER_FRAME, onChecnMessage);
		}
 
		//	设置游戏名称
		public function set gameName(str:String):void
		{
			this.nowGameName = str;
		}
		
		//	读取游戏名称
		public function get gameName():String
		{
			return this.nowGameName;
		}
		
		//	设置服务器连接对象
		public function set server(sp:AnsheNetSpace):void
		{
			this.ansheSpace = sp;
		}
		
		//	读取服务器对象
		public function get server():AnsheNetSpace
		{
			return this.ansheSpace;
		}		
		
		//	设置事件侦听
		public function AddListener(funcName:String, func:Function):void
		{
			if(this.server)
			{
				trace("添加注册侦听:" + funcName + " 成功");
				this.server[funcName] = func;
			}
		}
		
		//	向服务器发送命令消息
		public function SendCommand(actName:String, funcName:String, ...argc):void
		{
			var act:ActionStruct = new ActionStruct(msgArray.length);
			act.actClient = this;
			act.actString = actName;
			act.actFunction = funcName;
			act.actData = argc;
			act.isSoMessage = false;
			
			msgArray.push(act);
		}

		
		//	向服务器发送命令消息(消息形式为全局消息)
		public function GlobalCommand(actName:String, funcName:String, ...argc):void
		{
			var act:ActionStruct = new ActionStruct(msgArray.length);
			act.actClient = this;
			act.actString = actName;
			act.actFunction = funcName;
			act.actData = argc;
			act.isSoMessage = true;
			
			msgArray.push(act);
		}

		//	让游戏初始化完成的回调的函数
		public function InitGameListener():void
		{			
		}
		
		//	在 IMVU 里面说话
		public function Talk(str:String):void
		{
			if(ExternalInterface.available)
			{
				try
				{
					ExternalInterface.call("sendMessage", str);
				}
				catch(e:*)
				{
					ShowMessage("Can't Talk in IMVU");
				}
			}
			else
			{
				ShowMessage("Not Find Talk Function");
			}
		}

		/////////////////////////////////////////////////////////////////////
		
		//	对象显示到舞台上以后调用的方法
		private function onInitClient(e:Event):void
		{
			this.space = AnsheGlobal.GetAcsSprite(this);
			
			this.stage.addEventListener(AnsheNetEvent.SERVER_CONN_COMPLETE, onServerConnDone);
			this.stage.addEventListener(AnsheNetEvent.SERVER_CLOSE, onServerCloseEvent);
			this.stage.addEventListener(AnsheNetEvent.SERVER_CONN_ERROR, onServerConnError);
			this.stage.addEventListener(AnsheNetEvent.SO_RUSH_DATA, onRoomSoRushData);
			
			// trace("对象:" + this + "显示完毕,开始准备调用 InitGame");
			InitGame();
		}	
		
		//	检测是否有消息需要发送
		private function onChecnMessage(e:Event):void
		{
			if(this.server)
			{
				if(this.msgArray.length > 0 && this.server.ncConnected)
				{
					var act:ActionStruct = this.msgArray.shift();								
					this.server.sendCommand(this, act.actString, act.actFunction, act.isSoMessage, act.isGlobal, act.actData);	
				}
			}
		}
		
		//	当服务器连接成功时触发
		private function onServerConnDone(e:AnsheNetEvent):void
		{
			onServerConned();			
		}
		
		//	当服务器连接出错（含连接失败）时触发
		private function onServerConnError(e:AnsheNetEvent):void
		{
			onServerError();			
		}
		
		//	当服务器关闭时触发
		private function onServerCloseEvent(e:AnsheNetEvent):void
		{
			onServerClose();			
		}
		
		//	当房间 RedSharedObject 对象被刷新时触发
		private function onRoomSoRushData(e:AnsheNetEvent):void
		{
			onRushGlobalSO(e.data);
		}
		
		///////////////////////////////////////////////////////////////////
		
		//	对象显示到舞台上以后调用方法，子类必须重写的方法
		protected function InitGame():void
		{	
		}
		
		//	当服务器连接成功时调用
		protected function onServerConned():void
		{			
		}
		
		//	当服务器连接出错时调用
		protected function onServerError():void
		{			
		}
		
		//	当服务器关闭时调用
		protected function onServerClose():void
		{			
		}
		
		//	当 IMVU 房间 SharedObject 类对象信息被刷新时调用
		protected function onRushGlobalSO(...argc):void
		{			
		}
		
		////////////////////////////////////////////////////////////////////
		
		//	显示一条信息
		protected function ShowMessage(...argc):void
		{			
			try
			{
				if(!this.space.fDebug)
				{
					trace("ShowInfoForDebug:: " + argc);
				}
				else
				{
					if(this.space.fDebug is DisplayObject)
					{
						this.space.fDebug.traceError(argc);
					}
					else
					{
						trace("ShowInfoForDebug:: " + argc);
					}
				}
			}
			catch(e:*)
			{
				trace("Can't find Space Object");
			}//**/
		}
	}
}